<?php
namespace GracefulTheme\Service\GracefulTheme\Impl;

use Topxia\Service\Common\BaseService;
use Topxia\Common\ArrayToolkit;
use GracefulTheme\Service\GracefulTheme\GracefulThemeService;

class GracefulThemeServiceImpl extends BaseService implements GracefulThemeService
{
    public function isAllowedConfig()
    {
        $currentTheme = $this->getSettingService()->get('theme');
        if (in_array($currentTheme['name'], array('雅致简洁（商业主题）'))) {
            return true;
        }
        return false;
    }

    public function getThemeConfigByName($name)
    {
        $config = $this->getThemeConfigDao()->getThemeConfigByName($name);
        $defaultConfig = require __DIR__.'/../../../DefaultConfig.php';
        $allConfig = require __DIR__.'/../../../AllConfig.php';
        $config['allConfig'] = $allConfig;

        if (empty($config['config'])) {
            $config['config'] = $defaultConfig;
        }

        if (empty($config['confirmConfig'])) {
            $config['confirmConfig'] = $defaultConfig;
        }

        if (empty($config['name'])) {
            $currentTheme = $this->getSettingService()->get('theme');
            $config['name'] = $currentTheme['name'];
        }

        return $config;
    }

    public function getCurrentThemeConfig()
    {
        $currentTheme = $this->getSettingService()->get('theme');
        return $this->getThemeConfigByName($currentTheme['name']);
    }

    public function getCurrentThemeConfirmConfig()
    {
        $currentTheme = $this->getSettingService()->get('theme');
        return $this->getThemeConfigByName($currentTheme['name']);
    }

    public function saveCurrentThemeConfig($config)
    {
        $currentTheme = $this->getSettingService()->get('theme');
        $currentTheme = $this->getThemeConfigDao()->getThemeConfigByName($currentTheme['name']);

        if (empty($currentTheme)) {
            $currentTheme = $this->getSettingService()->get('theme');
            return $this->createThemeConfig($currentTheme['name'], $config);
        }
        return $this->editThemeConfig($currentTheme['name'], array(
            'config' => $config
        ));
    }

    public function saveConfirmConfig()
    {
        $currentTheme = $this->getCurrentThemeConfig();

        return $this->editThemeConfig($currentTheme['name'], array(
            'confirmConfig' => $currentTheme['config']
        ));
    }
    
    public function resetConfig()
    {
        $currentTheme = $this->getSettingService()->get('theme');

        $defaultConfig = require __DIR__.'/../../../DefaultConfig.php';

        return $this->saveCurrentThemeConfig($defaultConfig);
    }

    private function createThemeConfig($name, $config)
    {
        return $this->getThemeConfigDao()->addThemeConfig(array(
            'name' => $name,
            'config' => $config,
            'updatedTime' => time(),
            'createdTime' => time(),
            'updatedUserId' => $this->getCurrentUser()->id
        ));
    }

    private function editThemeConfig($name, $config)
    {
        $config['updatedTime'] = time();
        $config['updatedUserId'] = $this->getCurrentUser()->id;
        return $this->getThemeConfigDao()->updateThemeConfigByName($name, $config);
    }


    protected function getThemeConfigDao()
    {
        return $this->createDao('GracefulTheme:GracefulTheme.ThemeConfigDao');
    }

    protected function getSettingService()
    {
        return $this->createService('System.SettingService');
    }
}